<template>
    <div>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>门店编号</td>
                    <td><input type="text" v-model="add.sname"></td>
                </tr>
                <tr>
                    <td>门店名称</td>
                    <td><input type="text" v-model="add.name"></td>
                </tr>
                <tr>
                    <td>门店业态</td>
                    <td>
                        <select v-model="add.style">
                            <option value="">==请选择==</option>
                            <option value="精品超市">精品超市</option>
                            <option value="大卖场">大卖场</option>
                            <option value="便利店">便利店</option>
                            <option value="社会区">社会区</option>
                            <option value="农村店">农村店</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input type="radio" v-model="add.tyle" name="tyle" value="开业">开业
                        <input type="radio" v-model="add.tyle" name="tyle" value="闭店">闭店
                    </td>
                </tr>
                <tr>
                    <td>开业时间</td>
                    <td><input type="date" v-model="add.time"></td>
                </tr>
                <tr>
                    <td>门店电话</td>
                    <td><input type="text" v-model="add.tel"></td>
                </tr>
                <tr>
                    <td>店长</td>
                    <td><input type="text" v-model="add.pname"></td>
                </tr>
                <tr>
                    <td>店长电话</td>
                    <td><input type="text" v-model="add.ptel"></td>
                </tr>
                <tr>
                    <td>图片</td>
                    <td>
                        <input type="file" @change="imga">
                        <img :src="add.img" width="100" height="100" v-if="add.img != ''">
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="ok"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref, reactive, onMounted } from "vue";
import moment from 'moment';
import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const route = useRoute();

let add: any = reactive(
    {
        "sname": "",
        "name": "",
        "style": "",
        "tyle": "开业",
        "time": "",
        "tel": "",
        "pname": "",
        "ptel": "",
        "img": "",
        "isdel": true
    }
)
const ok = () => {
    if (add.sname == "") {
        alert("门店编号不能为空");
        return;
    }
    if (add.name == "") {
        alert("门店名称不能为空");
        return;
    }
    if (add.style == "") {
        alert("门店业态不能为空");
        return;
    }
    if (add.time == "") {
        alert("开业时间不能为空");
        return;
    }
    if (add.tel == "") {
        alert("门店电话不能为空");
        return;
    }
    if (add.pname == "") {
        alert("店长名称不能为空");
        return;
    }
    if (add.ptel == "") {
        alert("店长电话不能为空");
        return;
    }
    axios({
        url: 'https://localhost:7211/api/Emp/add',
        method: 'post',
        data: add
    })
        .then(res => {
            console.log(res);
            if (res.data > 0) {
                alert("添加成功");
                router.push("/Show");
            }
        })
        .catch(err => {
            console.log(err);

        })
}
const imga = (e: any) => {
    console.log(e);
    let obj = e.target.files[0];
    let f = new FormData();
    f.append('file', obj);
    axios({
        url: 'https://localhost:7211/api/Emp/ImgAdd',
        method: 'post',
        data: f
    })
        .then(res => {
            console.log(res);
            if (res.data == "格式不正确") {
                alert("格式不正确");
                return;
            }
            if (res.data =="大于1M")
            {
                alert("大于1M");
                return;
            }
            add.img = res.data;
        })
        .catch(err => {
            console.log(err);

        })
}
</script>

<style scoped></style>